package org.abc.fund.repository.funds;

import org.abc.fund.entity.funds.FundAnnouncement;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.time.LocalDate;
import java.util.List;
import java.util.Optional;

public interface FundAnnouncementRepository extends JpaRepository<FundAnnouncement, Long> {

    // 查询指定基金的最新一条公告
    List<FundAnnouncement> findTop1ByFundCodeOrderByIdDesc(String fundCode);

    Optional<FundAnnouncement> findTop1ByFundCodeOrderByPublishDateDesc(String fundCode);

    boolean existsByFundCodeAndPublishDateAndTitle(String fundCode, LocalDate publishDate, String title);

    Optional<FundAnnouncement> findFirstByFundCodeAndPublishDateAndTitle(String fundCode, LocalDate publishDate, String title);

    @Modifying
    @Query("DELETE FROM FundAnnouncement a WHERE a.publishDate < :cutoffDate")
    int deleteByPublishDateBefore(@Param("cutoffDate") LocalDate cutoffDate);
}
